// pages/user/register.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree:'/images/disagree.png',
    Rule:'',
    name:'',
    password:'',
    confirm:'',
    nick:'',
    mail:'',
    phone:'',
    checkStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  listerner_name: function (e) {
    this.setData({
      name: e.detail.value,
      nick: e.detail.value,
    })
  },
 
  listerner_mail: function (e) {
    this.setData({
      mail: e.detail.value,
    })
  },
  listerner_phone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  listerner_password: function (e) {
    this.setData({
      password: e.detail.value,
    })
  },
  listerner_confirm: function (e) {
    this.setData({
      confirm: e.detail.value,
    })
  },
  status: function () {
    if (this.data.checkStatus) {
      this.setData({
        agree: '/images/disagree.png',
        checkStatus: false
      })
    } else {
      this.setData({
        agree: '/images/agree.png',
        checkStatus: true
      })
    }
    //console.log(this.data.checkStatus);
  },
  /**
   * 注册
   */
  toRegister:function(e){
    var that=this;
    //console.log(this.data.checkStatus);
    if (this.data.name == undefined || this.data.name =="") {
      wx.showToast({
        title: "用户名不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.mail == undefined || this.data.mail == "") {
      wx.showToast({
        title: "邮箱不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.phone == undefined || this.data.phone == "") {
      wx.showToast({
        title: "手机不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.password == undefined || this.data.password == "") {
      wx.showToast({
        title: "密码不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.confirm == undefined || this.data.confirm == "") {
      wx.showToast({
        title: "确认密码不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.confirm != this.data.password)
    {
      wx.showToast({
        title: "两次密码不一致",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.checkStatus) {
      wx.showToast({
        title: "请同意注册协议",
        icon: 'none',
        duration: 2000
      })
      return;
    }

      wx.request({
        url: app.globalData.Url + "Api/User/Register", //仅为示例，并非真实的接口地址
        data: {
          name: this.data.name,
          password: this.data.password,
          nick: this.data.nick + "",
          mail: this.data.mail + "",
          phone: this.data.phone + "",
        },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          console.log(res.data);
          if (res.data.result) {
            var token = res.data.data.Token;
            console.log(token);
            app.globalData.userInfo = res.data.data;
            app.globalData.Token = token;

            try {
              wx.setStorageSync('Token', token);
              wx.setStorageSync('User', app.globalData.userInfo)
            } catch (e) {
            }

            console.log("login Token :" + app.globalData.Token);
            wx.reLaunch({
              url: '/pages/home/index'
            })
          }
          else {
            wx.showToast({
              title: res.data.data,
              icon: 'none',
              duration: 2000
            })
          }      
        }
      })
  }
})